package Servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import Dao.CourseResourceDao;
import Dao.LearningResourceDao;
import Dao.MyDownloadDao;
import Dao.MyExcellentCourse_studentDao;
import Entity.CourseResource;
import Entity.LearningResource;
import Entity.MyDownload;
import Entity.MyExcellentCourse_student;

/**
 * Servlet implementation class SmartDownloadServlet
 */
//@WebServlet("/SmartDownloadServlet")
public class SmartDownloadServlet extends HttpServlet {
	private ServletConfig config;
	private static final long serialVersionUID = 1L;
	
	final public void init(ServletConfig config) throws ServletException{
		this.config=config;
	}
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SmartDownloadServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");//解决乱码
		response.setContentType("text/html;charset=utf-8");//解决乱码
		response.setCharacterEncoding("utf-8");
		String myNo=request.getParameter("myNo");
		String studentNo=request.getParameter("studentNo");
		String resourceID=request.getParameter("resourceID");
		String courseID=request.getParameter("courseID");
		if(myNo!=null) {  //将下载的记录保存在myDownload表中
			LearningResource lr=new LearningResourceDao().queryLearningResourceByResourceID(resourceID);
			MyDownload md=new MyDownload(myNo,resourceID,lr.getLabel(),lr.getResourceName(),lr.getUploaderID(),lr.getUploadTime(),lr.getBriefIntroduce());
			new MyDownloadDao().addMyDownload(md);
		}
		if(studentNo!=null) {
			CourseResource cr=new CourseResourceDao().queryCourseResourceByCourseID(courseID);
			MyExcellentCourse_student mes=new MyExcellentCourse_student(studentNo,cr.getCourseID(),cr.getLabel(),cr.getCourseName(),
					cr.getUploaderID(),cr.getUploadTime(),cr.getBriefIntroduce());
			new MyExcellentCourse_studentDao().addMyExcellentCourse(mes);
		}
		//PrintWriter out=response.getWriter();
		SmartUpload smartUpload = new SmartUpload();
		//2、初始化创建的SmartUpload对象
	    smartUpload.initialize(config, request, response);
		 // 获取下载文件名
	    String fileName = request.getParameter("fileName");
	    String uploader = request.getParameter("uploader");
	    String fileSavePath = request.getServletContext().getRealPath(uploader);
	    String lastDownPath = fileSavePath + "\\" + fileName;  //最终下载路径
	    String contentType="MIME";  //文件类型为所以文件
		/*
		 * File file=new File(lastDownPath); String
		 * fileType=file.getName().substring(file.getName().lastIndexOf("."),file.
		 * getName().length()); //获取文件类型
		 */	    String dis=fileName;   //下载的目标
	    System.out.print("最终路径"+lastDownPath);
	    try {
	        smartUpload.downloadFile(lastDownPath,contentType,dis);             //进行下载
	        //out.print("下载成功");
	    } catch (SmartUploadException e) {
	        e.printStackTrace();
	    }
	    //out.flush();
		//out.close();
		
	}

}
